﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using ShopB2C.Entity;
using ShopB2C.Helper;
using ShopB2C.Interfaces;
using ShopB2C.ViewModels;
using ShopB2C.Enums;

namespace ShopB2C.Controllers.Brower {
    public class HomeController : BaseController {
        private static IUser _userAccess;
        private static IClassify _classify;
        private static IBrand _brand;
        private static IProductPreview _productView;
        private static IImageRepeat _ir;
        private static INews _news;
        private static IComment _comment;

        private static string _irPath;

        protected override void Initialize(RequestContext requestContext) {
            base.Initialize(requestContext);
            this.SiteMapPathAdd("首页");
            _irPath = this.Server.MapPath("~/OtherPictures/");
        }

        public HomeController() {
            _userAccess = DataBaseExecuter.CreateClassByName<IUser>("UserAccess");
            _brand = DataBaseExecuter.CreateClassByName<IBrand>("Brand");
            _classify = DataBaseExecuter.CreateClassByName<IClassify>("Classify");
            _productView = DataBaseExecuter.CreateClassByName<IProductPreview>("ProductPreView");
            _ir = DataBaseExecuter.CreateClassByName<IImageRepeat>("ImageRepeat");
            _news = DataBaseExecuter.CreateClassByName<INews>("News");
            _comment = DataBaseExecuter.CreateClassByName<IComment>("Comment");
        }
        /// <summary>
        /// 获取所有的图片轮换配置和列表
        /// </summary>
        /// <returns>JSON</returns>
        public JsonResult ImageRepeatLoad() {
            ImageRepeatViewModel irvm = new ImageRepeatViewModel() {
                Width = ConfigHelper.GetValue<int>(ConfigNodeNames.ImageRepeat, "width"),
                Height = ConfigHelper.GetValue<int>(ConfigNodeNames.ImageRepeat, "height"),
                Interval = ConfigHelper.GetValue<int>(ConfigNodeNames.ImageRepeat, "interval"),
                IsAutoPlay = ConfigHelper.GetValue(ConfigNodeNames.ImageRepeat, "isAutoPlay"),
                IsShowContent = ConfigHelper.GetValue(ConfigNodeNames.ImageRepeat, "isShowContent"),
                IsShowImgPage = ConfigHelper.GetValue(ConfigNodeNames.ImageRepeat, "isShowImgPage"),
                ImageRepeatList = _ir.GetImageRepeatList(string.Empty)
            };
            return Json(irvm, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 首页
        /// </summary>
        public ActionResult Index() {
            int pageCount;
            return View(new HomeViewModel() {
                BrandList = _brand.GetBrandList(string.Empty),
                NewsList = _news.GetNewsList(
                    ConfigHelper.GetValue<int>(ConfigNodeNames.News, "classify"),
                    ConfigHelper.GetValue<int>(ConfigNodeNames.News, "pagesize"),
                    1, out pageCount),
                ProductClassifyList = _classify.GetClassifyByModuleKey("Product"),
                ProductPart = _productView.GetPartList(true),
                CommentList = _comment.GetCommentHot(5) ?? new List<CommentEntity>(),
                Title = _title,
                Description = _description,
                Keywords = _keywords,
                ImageRoot = _imageRoot

            });
        }
        /// <summary>
        /// 关于
        /// </summary>
        public ActionResult About() {
            this.SiteMapPathAdd("关于");
            return View();
        }
        /// <summary>
        /// 登录
        /// </summary>
        public ActionResult Login() {
            this.SiteMapPathAdd("用户登录");
            return View();
        }
        /// <summary>
        /// 登录
        /// </summary>
        [HttpPost]
        public ActionResult Login(
            [DefaultValue("")]string uname
            , [DefaultValue("")]string password
            , [DefaultValue("")]string verifyCode
            , [DefaultValue(0)]int timeout
            , [DefaultValue("/")]string returnUrl) {
            MessageHelper msg = new MessageHelper("login");

            UserEntity user = _userAccess.Login(uname, password);
            msg.AddMessage(1);
            msg.AddBody(1);

            if (!object.Equals(user, null)) {
                this.WriteLoginCookie(user, timeout);
                return Redirect(returnUrl);
            }

            return MessageResult(msg.CreateMessage());

        }
        /// <summary>
        /// 退出
        /// </summary>
        public ActionResult LogOut([DefaultValue("/")]string returnUrl) {
            FormsAuthentication.SignOut();
            return Redirect(returnUrl);
        }
        /// <summary>
        /// 注册
        /// </summary>
        public ActionResult Register() {
            this.SiteMapPathAdd("用户注册");
            return View();
        }
        /// <summary>
        /// 注册
        /// </summary>
        [HttpPost]
        public ActionResult Register(UserEntity registerUser) {
            MessageHelper msgHelper = new MessageHelper("register");
            // 验证用户名不能为空
            msgHelper.Required(registerUser.Uname, 1);
            // 验证EMAIL格式不正确
            msgHelper.Email(registerUser.Uname, 2);
            // 验证用户昵称不能为空
            msgHelper.Required(registerUser.NickName, 4);
            // 验证密码不得小于6位
            msgHelper.MinLength(registerUser.Password, 6, 3);
            // 所有检查成功开始注册
            if (msgHelper.IsValid) {
                if (_userAccess.RegNewUser(registerUser) < 0) {
                    msgHelper.AddBody(1);
                    msgHelper.DefaultOperate(Url.Action("Login", "Home"));
                    msgHelper.AddMessage(5);
                }
                else {
                    msgHelper.AddMessage(6);
                }
            }
            else {
                msgHelper.AddBody(2);
                msgHelper.AddOperate("登陆", Url.Action("Login", "Home"));
                msgHelper.AddOperate("首页", Url.Action("Index", "Home"));
                msgHelper.AddOperate("新闻中心", Url.Action("Index", "News"));
            }
            return MessageResult(msgHelper.CreateMessage());

        }
        /// <summary>
        /// 密码找回
        /// </summary>
        public ActionResult ForgotPassword() {
            return View();
        }
        /// <summary>
        /// 密码找回
        /// </summary>
        [HttpPost]
        public ActionResult ForgotPassword([DefaultValue("")]string uname) {
            return View();
        }
        /// <summary>
        /// 全局错误页
        /// </summary>
        [HandleError]
        public ActionResult Error() {
            return View();
        }

    }
}